<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/20
 * Time: 9:39
 */

namespace App\Http\Controllers\Front;


use App\Http\Controllers\Controller;
use App\Model\CardFriendlyLinks;
use App\Model\CardHolder;
use App\Model\Cards;
use App\Model\CardsPictureIntroductions;
use App\Model\UserPopularity;
use App\Model\UserTemplate;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class CardController extends Controller
{
    /**
     * @SWG\Get(
     *     path="/front/user-info/{id}",
     *     tags={"用户相关"},
     *     summary="根据名片id查看名片信息",
     *     description="根据名片id查看名片信息",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", type="integer", description="名片id"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *               @SWG\Property(property="id", type="integer", description="名片id"),
     *               @SWG\Property(property="user_id", type="integer", description="用户id"),
     *               @SWG\Property(property="head_picture", type="string", description="头像"),
     *               @SWG\Property(property="logo", type="string", description="logo"),
     *               @SWG\Property(property="user_name", type="string", description="用户名字"),
     *               @SWG\Property(property="motto", type="string", description="个人箴言"),
     *               @SWG\Property(property="mobile_phone", type="string", description="手机号"),
     *               @SWG\Property(property="telephone", type="string", description="手机号"),
     *               @SWG\Property(property="company", type="string", description="公司"),
     *               @SWG\Property(property="industry", type="string", description="行业"),
     *               @SWG\Property(property="profession", type="string", description="职业"),
     *               @SWG\Property(property="sex", type="string", description="性别"),
     *               @SWG\Property(property="birthday", type="string", description="生日"),
     *               @SWG\Property(property="wechat_code", type="string", description="微信号"),
     *               @SWG\Property(property="qq", type="string", description="qq"),
     *               @SWG\Property(property="email", type="string", description="email"),
     *               @SWG\Property(property="website", type="string", description="网站"),
     *               @SWG\Property(property="province", type="integer", description="省份id"),
     *               @SWG\Property(property="city", type="integer", description="城市id"),
     *               @SWG\Property(property="area", type="integer", description="区域id"),
     *               @SWG\Property(property="address_detail", type="string", description="详细地址"),
     *               @SWG\Property(property="popularity", type="integer", description="人气"),
     *               @SWG\Property(property="saved", type="integer", description="保存数"),
     *               @SWG\Property(property="style", type="string", description="风格"),
     *               @SWG\Property(property="introduce", type="string", description="个人介绍"),
     *               @SWG\Property(property="open_phone", type="integer", description="是否公开手机1公开"),
     *               @SWG\Property(property="album", type="string", description="相册"),
     *               @SWG\Property(property="address", type="string", description="地区"),
     *          )
     *      )
     * )
     */
    public function getUserInfoCard($id)
    {
        $card = Cards::where('user_id', $id)->first();

        return $this->renderJson($card);
    }

    /**
     *
     * @SWG\Post(
     *     path="/front/update-user-info",
     *     summary="修改基本信息",
     *     tags={"用户相关"},
     *     description="修改基本信息",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="user_name", in="formData", required=true, type="string", description="用户名字"),
     *     @SWG\Parameter(name="mobile_phone", in="formData", required=true, type="string", description="手机"),
     *     @SWG\Parameter(name="head_picture", in="formData", required=true, type="string", description="头像"),
     *     @SWG\Parameter(name="logo", in="formData", type="string", description="logo"),
     *     @SWG\Parameter(name="motto", in="formData", type="string", description="个人箴言"),
     *     @SWG\Parameter(name="telephone", in="formData", type="string", description="手机号"),
     *     @SWG\Parameter(name="company", in="formData", type="string", description="公司"),
     *     @SWG\Parameter(name="industry", in="formData", type="string", description="行业"),
     *     @SWG\Parameter(name="profession", in="formData", type="string", description="职业"),
     *     @SWG\Parameter(name="sex", in="formData", type="string", description="性别"),
     *     @SWG\Parameter(name="birthday", in="formData", type="string", description="生日"),
     *     @SWG\Parameter(name="wechat_code", in="formData", type="string", description="微信号"),
     *     @SWG\Parameter(name="qq", in="formData", type="string", description="qq"),
     *     @SWG\Parameter(name="email", in="formData", type="string", description="email"),
     *     @SWG\Parameter(name="website", in="formData", type="string", description="网站"),
     *     @SWG\Parameter(name="province", in="formData", type="integer", description="省份id"),
     *     @SWG\Parameter(name="city", in="formData", type="integer", description="城市id"),
     *     @SWG\Parameter(name="area", in="formData", type="integer", description="区域id"),
     *     @SWG\Parameter(name="address_detail", in="formData", type="string", description="详细地址"),
     *     @SWG\Parameter(name="style", in="formData", type="string", description="风格"),
     *     @SWG\Parameter(name="introduce", in="formData", type="string", description="个人介绍"),
     *     @SWG\Parameter(name="open_phone", in="formData", type="integer", description="是否公开手机1公开"),
     *     @SWG\Parameter(name="album", in="formData", type="string", description="相册"),
     *     @SWG\Response(
     *         response=200,
     *         description="操作成功"
     *     )
     * )
     */
    public function updateUserInfoCard(Request $request)
    {
        $this->validate($request,[
            'user_name' => 'required',
            'mobile_phone' => [
                'required',
                'regex:/^1[345789][0-9]{9}$/'
            ],
//            'company' => 'required',
//            'industry' => 'required',
//            'profession' => 'required',

        ],[
            'required' => ':attribute不能为空',
            'regex' => ':attribute验证失败'
        ],[
            'user_name' => '用户名',
            'mobile_phone' => '手机号码',
//            'company' => '公司名称',
//            'industry' => '行业',
//            'profession' => '职业'
        ]);

        if (!$card = Cards::where('user_id', $request->user()->id)->first()){
            $card = new  Cards();
        }
        $card->fill($request->all());
        $card->user_id = $request->user()->id;
        if (!$card->save()){
            return $this->error('添加失败');
        }
        if ($request->has('portrait')){
            $card->head_picture = $request->input('portrait');
            $card->save();
        }
        Cache::forget('profile-'.$request->user()->id);
        return $this->success('保存成功');

    }

    /**
     * @SWG\Get(
     *     path="/front/card/info",
     *     tags={"用户相关"},
     *     summary="查看自己的名片信息",
     *     description="查看自己的信息",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *               @SWG\Property(property="card_id", type="integer", description="名片id"),
     *               @SWG\Property(property="portrait", type="string", description="头像"),
     *               @SWG\Property(property="logo", type="string", description="logo"),
     *               @SWG\Property(property="name", type="string", description="用户名字"),
     *               @SWG\Property(property="profession", type="string", description="职业"),
     *               @SWG\Property(property="has_holder", type="boolean", description="是否在我的名片夹"),
     *               @SWG\Property(property="company", type="string", description="公司"),
     *               @SWG\Property(property="industry", type="string", description="行业"),
     *               @SWG\Property(property="phone", type="string", description="电话"),
     *               @SWG\Property(property="sex", type="string", description="性别"),
     *               @SWG\Property(property="birthday", type="string", description="生日"),
     *               @SWG\Property(property="wechat_code", type="string", description="微信号"),
     *               @SWG\Property(property="qq", type="string", description="qq"),
     *               @SWG\Property(property="email", type="string", description="email"),
     *               @SWG\Property(property="website", type="string", description="网站"),
     *               @SWG\Property(property="motto", type="string", description="箴言"),
     *               @SWG\Property(property="province", type="integer", description="省份id"),
     *               @SWG\Property(property="city", type="integer", description="城市id"),
     *               @SWG\Property(property="area", type="integer", description="区域id"),
     *               @SWG\Property(property="address_detail", type="string", description="详细地址"),
     *               @SWG\Property(property="popularity", type="integer", description="人气"),
     *               @SWG\Property(property="saved", type="integer", description="保存数"),
     *               @SWG\Property(property="style", type="string", description="风格"),
     *               @SWG\Property(property="introduce", type="string", description="个人介绍"),
     *               @SWG\Property(property="open_phone", type="integer", description="是否公开手机1公开"),
     *               @SWG\Property(property="album", type="string", description="相册"),
     *               @SWG\Property(property="address", type="string", description="地区"),
     *               @SWG\Property(property="has_page", type="integer", description="是否有个人网页false|个人网页id"),
     *               @SWG\Property(property="page_type", type="integer", description="个人网页类型1固定，2自定义"),
     *               @SWG\Property(property="share_count", type="integer", description="分享量"),
     *               @SWG\Property(property="is_perfect", type="integer", description="是否完善信息"),
     *          )
     *      )
     * )
     */
    public function getUserCardInfo(Request $request)
    {
        $user_id = $request->user()->id;

        return $this->cardInfo($user_id);
    }

    /**
     * @SWG\Get(
     *     path="/front/card",
     *     tags={"用户相关"},
     *     summary="根据用户id查看信息",
     *     description="根据用户id查看名片信息",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="user_id", in="query", required=true, type="integer", description="用户id"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *               @SWG\Property(property="card_id", type="integer", description="名片id"),
     *               @SWG\Property(property="portrait", type="string", description="头像"),
     *               @SWG\Property(property="logo", type="string", description="logo"),
     *               @SWG\Property(property="name", type="string", description="用户名字"),
     *               @SWG\Property(property="profession", type="string", description="职业"),
     *               @SWG\Property(property="has_holder", type="boolean", description="是否在我的名片夹"),
     *               @SWG\Property(property="company", type="string", description="公司"),
     *               @SWG\Property(property="industry", type="string", description="行业"),
     *               @SWG\Property(property="phone", type="string", description="电话"),
     *               @SWG\Property(property="sex", type="string", description="性别"),
     *               @SWG\Property(property="birthday", type="string", description="生日"),
     *               @SWG\Property(property="wechat_code", type="string", description="微信号"),
     *               @SWG\Property(property="qq", type="string", description="qq"),
     *               @SWG\Property(property="email", type="string", description="email"),
     *               @SWG\Property(property="website", type="string", description="网站"),
     *               @SWG\Property(property="motto", type="string", description="箴言"),
     *               @SWG\Property(property="province", type="integer", description="省份id"),
     *               @SWG\Property(property="city", type="integer", description="城市id"),
     *               @SWG\Property(property="area", type="integer", description="区域id"),
     *               @SWG\Property(property="address_detail", type="string", description="详细地址"),
     *               @SWG\Property(property="popularity", type="integer", description="人气"),
     *               @SWG\Property(property="saved", type="integer", description="保存数"),
     *               @SWG\Property(property="style", type="string", description="风格"),
     *               @SWG\Property(property="introduce", type="string", description="个人介绍"),
     *               @SWG\Property(property="open_phone", type="integer", description="是否公开手机1公开"),
     *               @SWG\Property(property="album", type="string", description="相册"),
     *               @SWG\Property(property="address", type="string", description="地区"),
     *               @SWG\Property(property="has_page", type="integer", description="是否有个人网页false|个人网页id"),
     *               @SWG\Property(property="page_type", type="integer", description="个人网页类型1固定，2自定义"),
     *               @SWG\Property(property="share_count", type="integer", description="分享量"),
     *               @SWG\Property(property="is_perfect", type="integer", description="是否完善信息"),
     *               @SWG\Property(property="vip_level", type="integer", description="vip等级"),
     *               @SWG\Property(property="user_type", type="integer", description="用户类型"),
     *          )
     *      )
     * )
     */
    public function getCard()
    {
        $this->validate(request(), [
            'user_id' => 'required'
        ], [
            'user_id.required' => '用户id不能为空'
        ]);
        return $this->cardInfo(request('user_id'));
    }

    private function cardInfo($user_id)
    {
        $user = User::find($user_id);
        if (is_null($user)){
            return $this->fail('用户不存在');
        }
        $card = Cards::where('user_id', $user_id)->first();
        $temp = UserTemplate::where('user_id', $user_id)->where('is_default', 1)->first();
        if (empty($temp)) {
            $temp = DB::table('personal_page_template_user')->where('user_id', $user_id)->orderByDesc('created_at')->first();
        }
        if ($card == null){
            $card = new Cards();
            $card->user_id = $user_id;
            $card->save();
			//人气记录
			UserPopularity::add(UserPopularity::CARD,$user_id,$card->id);
            return $this->renderJson([
                'has_page' => $temp?$temp->id:false,
                'portrait' => $user->detail?$user->detail->head_portrait:'',
                'name' => $user->detail?$user->detail->wechat_name:'',
                'card_id' => $card->id,
                'user_id' => $card->user_id,
            ]);
        }
        $my = CardsPictureIntroductions::where('user_id', $user_id)->where('type',1)->first();
        $c = CardsPictureIntroductions::where('user_id', $user_id)->where('type',2)->first();
        $p = CardsPictureIntroductions::where('user_id', $user_id)->where('type',3)->get();
        $f = CardFriendlyLinks::where('user_id',$user_id)->get();

        $data = [
            'card_id' => $card->id,
            'user_id' => $user_id,
            'portrait' => $user->detail?$user->detail->head_portrait:$card->head_picture,
            'logo' => $user->detail?$user->detail->head_portrait:$card->head_picture,
            'name' => $user->detail?$user->detail->wechat_name:'',
            'profession' => $card->profession,
            'has_holder' => false,
            'industry' => $card->industry,
            'company' => $card->company,
            'phone' => $card->mobile_phone,
            'email' => $card->email,
            'qq' =>$card->qq,
            'sex' => $card->sex==1?'男':'女',
            'birthday' => $card->birthday,
            'wechat' => $card->wechat_code,
            'motto' => $card?$card->motto:'',
            'website' =>$card->website,
            'address' => $card->address,
            'address_detail' => $card->address_detail,
            'has_page' => $temp?$temp->id:false,
            'page_type' => $temp?$temp->type:false,
            'self_introduction' => [
                'content' => $my?$my->content:'',
                'images' => $my?$my->picture_path:''
            ],
            'company_profile' => [
                'content' => $c?$c->content:'',
                'images' => $c?$c->picture_path:''
            ],
            'product_introduction' => [

            ],
            'friend_link' => [

            ],
            'style' => $card->style,
            'introduce' => $card->introduce,
            'album' => $card->album,
            'open_phone' => $card->open_phone,
            'share_count' => $card->share,
            'province' => $card->province,
            'city' => $card->city,
            'area' => $card->area
        ];
        if ($card->logo) {
            $data['logo'] = $card->logo;
        }
        //隐藏手机号
        if ($data['open_phone'] == 0) {
            $data['phone'] = substr_replace($data['phone'], '****', 3, 4);
        }
        if ($card->user_name){
            $data['name'] = $card->user_name;
        }
        if (Auth::check()){
            //登录状态
            //判断名片是否在我名片夹
            $holder = CardHolder::where('card_id', $card->id)->where('user_id', Auth::id())->first();
            if ($holder){
                $data['has_holder'] = true;
            }
        }

        if (!empty($card)){
            //如果修改头像就使用新头像
            if ($card->head_picture){
                $data['portrait'] = $card->head_picture;
            }
        }
        foreach ($p as $t){
            $data['product_introduction'][] = [
                'title' => $t->title,
                'content' => $t->content,
                'images' => $t->picture_path
            ];
        }
        foreach ($f as $v){
            $data['friend_link'][] = [
                'id' => $v->card->id,
                'portrait' => $v->card->protrait,
                'name' => $v->card->user_name
            ];
        }
        $is_perfect = 1;
        if ($card == null || empty($card->user_name)){
            $is_perfect = 0;
        }
        $data['is_perfect'] = $is_perfect;
        $pop = true;
        if (Auth::id() == $user_id){
            $pop = false;
            //自己访问显示手机号
            $data['phone'] = $card->mobile_phone;
        }
        //人气
        if ($pop){
            //如果已登录判断是否是自己访问
            $card->increment('popularity');
            $card->save();
        }
		//人气记录
        UserPopularity::add(UserPopularity::CARD,$user_id,$card->id);
        $data['vip_level'] = User::is_vip($user_id);
        $data['user_type'] = $user->type;
        return $this->renderJson($data);
    }



}